//
//  EmoticonView.swift
//  七哥直播
//
//  Created by seven on 2017/12/26.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit

private let KcellIdentifer = "emoticonCell"
class EmoticonView: UIView {

    var emoticonClickCallback: ((_ emoticon: Emoticon) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension EmoticonView{
    
    private func setupUI(){
        //1.创建LXpagecollectionView
        let style = LXTitleStyle()
        style.isShowIndicatorLine = true
        let layout = LXPageCollectionViewLayout()
        layout.rows = 3
        layout.cols = 7
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        let pageFrame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height - kBottomSafeArea)
        let pageCollectionView = LXPageCollectonView(frame: pageFrame, titles: ["普通表情","粉丝专属"], style: style, isTitleInTop: false, layout: layout)
        addSubview(pageCollectionView)
        
        pageCollectionView.dataSource = self
        pageCollectionView.delegate = self
        pageCollectionView.register(nib: UINib(nibName: "EmoticonViewCell", bundle: nil), identifier: KcellIdentifer)
    }
    
}

extension EmoticonView: LXPageCollectionViewDataSource,LXPageCollectonViewDelegate{
    
    func numberOfSections(in pageCollectionView: LXPageCollectonView) -> Int {
        return EmoticonViewModel.shared.packages.count
    }
    
    func pageCollectionView(_ collectionView: LXPageCollectonView, numberOfItemsInSection section: Int) -> Int {
        return EmoticonViewModel.shared.packages[section].emoticons.count
    }
    
    func pageCollectionView(_ pageCollectionView: LXPageCollectonView, _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KcellIdentifer, for: indexPath) as! EmoticonViewCell
        cell.emoticon = EmoticonViewModel.shared.packages[indexPath.section].emoticons[indexPath.row]
        return cell
    }
    
    func pageCollectionView(_ collectionView: LXPageCollectonView, didSelectItemAt indexPath: IndexPath) {
        let emoticon = EmoticonViewModel.shared.packages[indexPath.section].emoticons[indexPath.row]
        if let callback = emoticonClickCallback {
            callback(emoticon)
        }
    }
}
